package com.itzxx.huazhun.ui.record.valuation

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.github.aachartmodel.aainfographics.aachartcreator.AAChartModel
import com.github.aachartmodel.aainfographics.aachartcreator.AAChartType
import com.itzxx.huazhun.data.model.EventMessage
import com.itzxx.huazhun.data.model.EventType
import com.itzxx.huazhun.databinding.ActivityRecordValuationBinding
import com.itzxx.library_base_ktx.base.activity.BaseVmDbActivity
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class RecordValuationActivity :
    BaseVmDbActivity<RecordValuationViewModel, ActivityRecordValuationBinding>() {

    companion object {
        fun startActivity(context: Context, type: Int, list: ArrayList<Int>, equipmentId: String) {
            val instent = Intent(
                context,
                RecordValuationActivity()::class.java
            )
            instent.putExtra("type", type)
            instent.putExtra("equipmentId", equipmentId)
            instent.putIntegerArrayListExtra("list", list)
            context.startActivity(
                instent
            )
        }
    }

    lateinit var aaChartModel: AAChartModel

    override fun initView(savedInstanceState: Bundle?) {
        EventBus.getDefault().register(this)
        setSupportActionBar(mDatabind.tbHead)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        aaChartModel = AAChartModel()
            .chartType(AAChartType.Line)
            .backgroundColor("#ffffff")
            .dataLabelsEnabled(false)
            .legendEnabled(false)
            .yAxisTitle("")
        intent?.run {
            val parcelableArrayListExtra = getIntegerArrayListExtra("list")
            val intExtra = getIntExtra("type", 0)
            val equipmentId = getStringExtra("equipmentId")
            parcelableArrayListExtra?.let {
                mViewModel.getCloundData(intExtra, it, equipmentId)
            }
        }
    }

    override fun createObserver() {
        mViewModel.run {
            curveDataCategoriesLiveData.observe(this@RecordValuationActivity) {
                runOnUiThread {
                    aaChartModel.categories(it.toTypedArray())
                    mDatabind.aaChartView.aa_drawChartWithChartModel(aaChartModel)
                }

            }

            curveDataLiveData.observe(this@RecordValuationActivity) {
                runOnUiThread {
                    aaChartModel.series(it.toArray())
                    mDatabind.aaChartView.aa_drawChartWithChartModel(aaChartModel)
                }
            }
        }
    }

    override fun databind2ViewModel() {
        super.databind2ViewModel()
        mDatabind.viewmodel = mViewModel
    }

    override fun onSupportNavigateUp(): Boolean {
        finish()
        return true
    }

    override fun isApplyDefaultWaitDialog(): Boolean {
        return true
    }

    //接收事件
    @Subscribe(threadMode = ThreadMode.MAIN, priority = 1)
    fun onReceiveMsg(message: EventMessage) {
        if (EventType.UPLOAD_RECORD == message.type) {
            mViewModel.requestDetail(message.arg1)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

}